<?php
require_once 'Zend/Controller/Action.php';
require_once 'BaseView.php';
require_once 'Xiachufang/logic/Actor.php';
class BaseController extends Zend_Controller_Action  {
	/**
	 * @var ActorEntity
	 */
	protected $_actor;
	protected $_actorId;
	protected $_pi;
	
	/**
	 * @var Smarty
	 */
	protected $smarty;
	function init() {
		$this->smarty = new BaseView();
		$this->loadCurActor();
		if ($this->_actor) {
			$this->smarty->assign("loginActor", $this->_actor->getDataArray(array(
				"actorId","email","nickname"
			)));
		}
		$this->smarty->assign(array(
			"curUrl"=> isset($_REQUEST["rurl"]) ? $_REQUEST["rurl"] : $_SERVER["REQUEST_URI"],
		));
		$this->_pi = (int)$this->_getParam('page', 1) - 1;
		parent::init();
		//var_dump($this->_request);
	}	
	
	/**
	 * 得到当前用户
	 *
	 * @return unknown
	 */
	private function loadCurActor() {
		$al = new ActorLogic();
		$this->_actor = $al->decodeActor();
		if ($this->_actor !== false) $this->_actorId = $this->_actor->actorId;
	}
	
	/**
	 * 检查是否登陆
	 *
	 */
	protected function checkLogin($rurl = null) {
		if (!$this->_actor) {
			if (empty($rurl)) $rurl = $_SERVER['REQUEST_URI'];
			$this->_redirect('login?rurl='.$rurl);
		}
	}
	
	/**
	 * 给视图分配变量
	 *
	 * @param array|string $key
	 * @param mixed $value
	 */
	function assign($key, $value) {
		$this->smarty->assign($key, $value);
	}
	
	/**
     * executes & displays the template results
     *
     * @param string $resource_name
     * @param string $cache_id
     * @param string $compile_id
     */
    function display($resource_name = null, $cache_id = null, $compile_id = null) {
    	if (empty($resource_name)) $resource_name = $this->_request->getControllerName().'/'.$this->_request->getActionName().'.tpl';
		$this->smarty->display($resource_name, $cache_id, $compile_id);
	}
}
?>